package com.frank.auth.application.controller;

import com.frank.auth.application.convert.AuthPermissionDtoConvert;
import com.frank.auth.application.dto.AuthPermissionDto;
import com.frank.auth.domain.bo.AuthPermissionBo;
import com.frank.auth.domain.service.PermissionDomainService;
import com.frank.common.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/13
 * @description
 */

@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    private final PermissionDomainService permissionDomainService;

    public PermissionController(PermissionDomainService permissionDomainService) {
        this.permissionDomainService = permissionDomainService;
    }

    @PostMapping(value = "/add", consumes = "application/json")
    public Result<Boolean> addPermission(@RequestBody AuthPermissionDto authPermissionDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController add permission, params: {}", authPermissionDto);
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDto.getPermissionKey()), "The permission key cannot be empty!");
            Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDto.getName()), "The permission name cannot be empty!");
            AuthPermissionBo authPermissionBo = AuthPermissionDtoConvert.INSTANCE.convertDtoToBo(authPermissionDto);
            return Result.ok(permissionDomainService.addPermission(authPermissionBo));
        } catch (Exception e) {
            log.error("PermissionController add permission error, params: {}", authPermissionDto);
            return Result.fail("添加权限失败： " + e.getMessage());
        }
    }

    @PutMapping(value = "/update", consumes = "application/json")
    public Result<Boolean> updatePermission(@RequestBody AuthPermissionDto authPermissionDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController add permission, params: {}", authPermissionDto);
            }
            Preconditions.checkNotNull(authPermissionDto.getId(), "The permission id cannot be empty!");
            AuthPermissionBo authPermissionBo = AuthPermissionDtoConvert.INSTANCE.convertDtoToBo(authPermissionDto);
            return Result.ok(permissionDomainService.updatePermission(authPermissionBo));
        } catch (Exception e) {
            log.error("PermissionController add permission error, params: {}", authPermissionDto);
            return Result.fail("添加权限失败： " + e.getMessage());
        }
    }

    @DeleteMapping(value = "/delete/{id}", consumes = "application/json")
    public Result<Boolean> deletePermission(@PathVariable Integer id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController add permission, id: {}", id);
            }
            return Result.ok(permissionDomainService.deletePermission(Long.valueOf(id)));
        } catch (Exception e) {
            log.error("PermissionController add permission error, id: {}", id);
            return Result.fail("添加权限失败： " + e.getMessage());
        }
    }

    @GetMapping("/getPermission")
    public Result<List<String>> getPermission(String username){
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController getPermission, username: {}", username);
            }
            return Result.ok(permissionDomainService.getPermission(username));
        } catch (Exception e) {
            log.error("PermissionController getPermission error, id: {}", username, e);
            return Result.fail("失败： " + e.getMessage());
        }
    }

}
